package pl.register.android.database;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import pl.register.android.database.crud.*;
import pl.register.android.database.dao.*;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-15
 */
public class DatabaseAdapter {
    private static final String DEBUG_TAG = DatabaseAdapter.class.getSimpleName();

    private static final String DATABASE_NAME = "course_register_database.db";

    private static final int DB_VERSION = 1;

    private SQLiteOpenHelper dbHelper;

    private SDCardDatabaseHelper sdDbHelper;

    private SQLiteDatabase database;


    private CourseCRUD courseCRUD;
    private YearCRUD yearCRUD;
    private StudentGroupCRUD groupCRUD;
    private StudentsCRUD studentsCRUD;
    private StudentLessonCRUD studentLessonCRUD;
    private ProgramCRUD programCRUD;
    private TaskCRUD taskCRUD;

    public DatabaseAdapter(Context context) {
//        this.dbHelper = new DatabaseHelper(context, DATABASE_NAME, DB_VERSION);
        this.sdDbHelper = new SDCardDatabaseHelper();
    }

    public SQLiteDatabase open() {
        database = sdDbHelper.getWritableDatabase();
        courseCRUD = new CourseCRUD(database);
        yearCRUD = new YearCRUD(database);
        groupCRUD = new StudentGroupCRUD(database);
        studentsCRUD = new StudentsCRUD(database);
        programCRUD = new ProgramCRUD(database);
        taskCRUD = new TaskCRUD(database);
        studentLessonCRUD = new StudentLessonCRUD(database);
        return database;
    }

    public CourseDao getCourseDao() {
        return courseCRUD;
    }

    public YearDao getStudentYearDao() {
        return yearCRUD;
    }

    public StudentGroupDao getStudentGroupDao() {
        return groupCRUD;
    }

    public StudentsDao getStudentDao() {
        return studentsCRUD;
    }

    public ProgramDao getProgramDao() {
        return programCRUD;
    }

    public StudentLessonDao getStudentLessonDao() {
        return studentLessonCRUD;
    }

    public TaskDao getTaskDao() {
        return taskCRUD;
    }

    public void close() {
        database.close();
    }
}